<template>
  <el-dialog
    title="收货地址"
    :visible.sync="isVisible"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @opened="opened"
  >
    <el-form :model="activity">
      <el-form-item label="活动名称" :label-width="formLabelWidth">
        <el-input v-model="activity.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="活动展示图" :label-width="formLabelWidth">
        <el-upload
          ref="upload"
          action="/upload"
          :multiple="false"
          :limit="1"
          :http-request="onFileUpload"
        >
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip" v-if="activity.picture == null">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="活动名称" :label-width="formLabelWidth">
        <div ref="contentEditor"></div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('activity-created', false)">取 消</el-button>
      <el-button type="primary" @click="$emit('activity-created', true)">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import E from 'wangeditor';
export default {
  props: {
    isVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      activity: {
        name: '',
        picture: null, //互动封面图
        content: '',
        table: '',
        description: '',
        date_begin: '',
        date_end: '',
        reapply: false,
        if_show: false,
        if_only_show: false
      },
      formLabelWidth: '100px',
      isDialogVisible: this.isVisible,
      contentEditor: null
    };
  },
  methods: {
    opened() {
      this.createEditor(this.contentEditor, (html) => {
        this.activity.content = html;
      });
    },
    createEditor(editor, htmlChange) {
      if (editor == null) {
        editor = new E(this.$refs.contentEditor);
        // 编辑器的事件，每次改变会获取其html内容
        editor.config.onchange = htmlChange;
        editor.config.height = 100;
        editor.config.menus = [
          // 菜单配置
          'head', // 标题
          'bold', // 粗体
          'fontSize', // 字号
          'fontName', // 字体
          'italic', // 斜体
          'underline', // 下划线
          'strikeThrough', // 删除线
          'foreColor', // 文字颜色
          'backColor', // 背景颜色
          'link', // 插入链接
          'list', // 列表
          'justify', // 对齐方式
          'quote', // 引用
          'emoticon', // 表情
          'image', // 插入图片
          'table' // 表格
        ];
        editor.create(); // 创建富文本实例
        return editor;
      }
    },
    onActivityCreate() {
      //todo: 创建activity并关闭dialog
      this.$emit('activity-created', true);
    },
    onFileUpload({action, file}) {
      let fd = new FormData();
      fd.append('file', file);
      this.request({
        url: action,
        method: 'post',
        headers: {'Content-Type': 'multipart/form-data'},
        data: fd
      }).then((resp) => {
        const data = resp.data;
        this.activity.picture = data.url;
      });
    }
  }
};
</script>